package com.eventosgasteiz.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class LoadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {

  public interface LoadImageAsyncTaskResponder {
    public void imageLoading();
    public void imageLoadCancelled();
    public void imageLoaded(Bitmap bitmap);
  }

  private LoadImageAsyncTaskResponder responder;
  private final String URLBASE = ImageHandler.extStorageDirectory;
  
  public LoadImageAsyncTask(LoadImageAsyncTaskResponder responder) {
    this.responder = responder;
  }

  @Override
  protected Bitmap doInBackground(String... urls) {   
    	Bitmap bitmap = null;
		for (String url : urls) {
			if(url!=null){
				bitmap = BitmapFactory.decodeFile(URLBASE + url);				
			}
		}			
		return bitmap;    
  }

  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    responder.imageLoading();
  }

  @Override
  protected void onCancelled() {
    super.onCancelled();
    responder.imageLoadCancelled();
  }

  @Override
  protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);
    responder.imageLoaded(result);
  }

}

